import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ebook/model/activity.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';

class BookActivity extends StatelessWidget {
  final List<Activity> activities;
  const BookActivity({super.key, required this.activities});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _buildTitle(),
        //轮播图
        10.verticalSpace,
        _buildSwiper(),
      ],
    );
  }

  Widget _buildTitle() {
    return Text(
      "读书活动",
      style: TextStyle(fontSize: 24.sp, fontWeight: FontWeight.w600),
    );
  }

  Widget _buildSwiper() {
    return SizedBox(
      height: 90.h,
      child: Swiper(
          pagination: SwiperPagination(
              alignment: Alignment.bottomRight,
              builder: DotSwiperPaginationBuilder(
                color: Colors.white.withOpacity(0.4),
                activeColor: Colors.white,
                size: 8.0,
                activeSize: 10.0,
                space: 2.0,
              )),
          itemCount: activities.length,
          itemBuilder: (context, index) {
            return Stack(
              //Stack实现图片和文字叠加效果
              children: [
                Container(
                  height: 90.h,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(15.r),
                    image: DecorationImage(
                      image: CachedNetworkImageProvider(
                          activities[index].cover ?? "",
                          headers: const {
                            "User-Agent":
                                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
                          }),
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                // 背景
                Container(
                  width: double.infinity,
                  height: 90.h,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(15.r),
                    color: Colors.black.withOpacity(0.5),
                  ),
                ),
                // 文字
                Container(
                  height: 90.h,
                  padding: EdgeInsets.all(15.r),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      // 标题
                      Text(
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis, //超出部分省略
                        activities[index].title ?? "",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 36.sp,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      // 专题时间
                      Row(
                        children: [
                          Container(
                            padding: EdgeInsets.all(5.r),
                            decoration: BoxDecoration(
                              color: Colors.white.withOpacity(0.2),
                              borderRadius: BorderRadius.circular(4.r),
                            ),
                            child: Text(
                              activities[index].label ?? "",
                              style: TextStyle(
                                fontSize: 20.sp,
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ),
                          20.horizontalSpace,
                          // 时间
                          Text(
                            activities[index].time ?? "",
                            style: TextStyle(
                              fontSize: 20.sp,
                              color: Colors.white,
                              fontWeight: FontWeight.bold,
                            ),
                          )
                        ],
                      )
                    ],
                  ),
                )
              ],
            );
          }),
    );
  }
}
